import sys
import pandas as pd
from PyQt5 import QtCore, QtGui
from PyQt5.QtCore import QAbstractTableModel, Qt
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

import tushare as ts
import datetime

# df = pd.DataFrame({'a': ['Mary', 'Jim', 'John'],
#                    'b': [100, 200, 300],
#                    'c': ['a', 'b', 'c']})

# 股票列表数据
# someday = (datetime.date.today() + datetime.timedelta(-1)).strftime('%Y%m%d')
# pro = ts.pro_api()
# data = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,market')
# df = pro.daily(trade_date=someday)
# stockData = pd.merge(data, df)[
#     ['ts_code', 'symbol', 'name', 'pct_chg', 'close', 'change', 'open', 'high', 'low', 'vol', 'amount', 'market']
# ].sort_index()


class PandasModel(QAbstractTableModel):

    def __init__(self, data, header):
        QAbstractTableModel.__init__(self)
        self.data = data
        self.header = header

    def rowCount(self, parent=None):
        return self.data.shape[0]

    def columnCount(self, parnet=None):
        return self.data.shape[1]

    def data(self, index, role=Qt.DisplayRole):
        if role == Qt.TextAlignmentRole:
            return Qt.AlignCenter  # 居中
        if index.isValid():
            if role == Qt.DisplayRole:  # 展示每格数据
                if index.column() == 9:  # 【成交手】的显示格式: 整数
                    return str(int(self.data.iloc[index.row(), index.column()]))
                elif index.column() == 10:  # 【成交额】的显示格式: xx万
                    return str(int(self.data.iloc[index.row(), index.column()]) // 1000) + "万"
                else:
                    return str(self.data.iloc[index.row(), index.column()])
            if role == Qt.TextColorRole:  # 文本颜色
                if index.column() == 0:
                    return QColor(0, 0, 0)
                if index.column() == 1 or index.column() == 2:  # 设置0,1,2列为蓝色
                    return QColor(0, 0, 255)
                if self.data.iloc[index.row(), 3] > 0:  # 用每行第4列的[涨幅]来判断颜色
                    return QColor(255, 0, 0)
                elif self.data.iloc[index.row(), 3] < 0:
                    return QColor(0, 150, 0)
                else:
                    return QColor(0, 0, 255)
            # if role == Qt.FontRole:  # 文本字体
            #     if self.data.iloc[index.row(), index.column()] > 0:
            #         return QFont('微软雅黑', 15)
            # if role == Qt.BackgroundColorRole:  # 设置背景颜色
            #     return QColor(Qt.red)
        return None

    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self.header[col]
        return None


# if __name__ == '__main__':
    # headers = ['代码', '股市代码', '名称', '涨幅%', '收盘', '涨跌', '开盘', '最高价', '最低价', '成交量(手)', '成交额', '市场']  # 表头标题列表
    #
    # app = QApplication(sys.argv)
    # model = PandasModel(stockData, headers)
    # view = QTableView()
    # view.setModel(model)
    # view.setColumnHidden(0, True)  # 隐藏 ts_code 列
    # view.setColumnHidden(11, True)  # 隐藏 market 列
    # view.resize(1600, 600)
    # view.show()
    # sys.exit(app.exec_())
